package com.fhx.qml.controller;

import com.fhx.qml.entity.User;
import com.fhx.qml.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/add")
    public User add(String name, String brith){
        User user=new User();
        user.setName(name);
        LocalDateTime dt = LocalDateTime.parse(brith);
        user.setBirth(dt);
        userMapper.insert(user);
        return user;
    }

    @RequestMapping("search")
    public List<User> search(){
        List<User> fhx=userMapper.selectList(null);
        return fhx;
    }

    @RequestMapping("delete")
    public void delete(Long id){
        userMapper.deleteById(id);
    }

    @RequestMapping("update")
    public  void update(Long id,String name,String brith){
        User user=new User();
        user.setId(id);
        user.setName(name);
        if(brith!=null){
            LocalDateTime dt = LocalDateTime.parse(brith);
            user.setBirth(dt);
        }
        userMapper.updateById(user);
    }
}
